<?php
/**
 * (logic)（临时）销售明细费用
 */
namespace App\Logic\File\ProfitLT;

use App\Models\MonthlyReportLT\Tmp\TmpSales;

class ParseSalesFile
{
    /**
     * 解析SalesFile
     * @param $date
     * @param $lines
     * @return int
     */
    public static function exeParse($lines,$date): int
    {
        $count = count($lines);
        $i = 0;
        $num = 100;
        $re_count = 0;
        while(true){
            if(empty($lines))break;
            $_lines = array_splice($lines,$i,$num);
            foreach ($_lines as $line){
                $flag = self::_addSales($line,$date);
                if(!$flag){
                    echo $line[0].'==='.$line[4]."\n";
                }
                $flag && $re_count++;
            }
        }
        return $re_count == $count ? 1 : 0;
    }

    private static function _addSales($temp,$date): bool
    {
        if(count($temp) < 5)return true;
        if(!$temp[0])return true;
        $re = TmpSales::query()->create([
            'report_date' => $date,
            'order_id' => trim($temp[0]),
            'th_order_id' => trim($temp[1]),
            'product_sales' => $temp[2],
            'quantity' => $temp[3],
            'sku' => trim($temp[4]),
        ]);
        return (bool)$re;
    }
}
